package com.itany.corejava.day12;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestMouseEvent {
    private JFrame jf;
    private JLabel jl;
    private JTextArea jta;

    public TestMouseEvent() {
        jf=new JFrame();
        jl=new JLabel();
        jta=new JTextArea(20,40);
    }


    private void init(){
        JScrollPane jsp=new JScrollPane(jta);
        jf.add(jl, BorderLayout.NORTH);
        jf.add(jsp);
    }

    private void setStyle(){
        jta.setFont(new Font("宋体",Font.BOLD,26));
        jta.setForeground(Color.RED);
    }


    private void addEventHandler(){
//        jta.addMouseListener(new MouseListener() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//
//            }
//
//            @Override
//            public void mousePressed(MouseEvent e) {
//
//            }
//
//            @Override
//            public void mouseReleased(MouseEvent e) {
//
//            }
//
//            @Override
//            public void mouseEntered(MouseEvent e) {
//
//            }
//
//            @Override
//            public void mouseExited(MouseEvent e) {
//
//            }
//        });



        jta.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                jta.append("鼠标进入\n");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                jta.append("鼠标移出\n");
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                String keyName=null;
                switch (e.getButton()){
                    case MouseEvent.BUTTON1 :
                        keyName="左";
                        break;
                    case MouseEvent.BUTTON2 :
                        keyName="中";
                        break;
                    case MouseEvent.BUTTON3 :
                        keyName="右";
                        break;
                }
                jta.append("鼠标"+keyName+"键被点击("+e.getX()+","+e.getY()+")\n");
            }
        });


        jta.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                jl.setText("("+e.getX()+","+e.getY()+")");
            }
        });
    }

    public void show(){
        init();
        setStyle();
        addEventHandler();
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestMouseEvent().show();
    }

























}
